package com.bill.common.system.dao;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.bill.common.user.dto.RegisterCodeDTO;
import com.bill.common.user.entity.RegisterCode;
import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.util.StringUtils;

public class GenerateRegisterCodeDao extends BaseDAOImplHibernate<RegisterCode>
implements IGenerateRegisterCodeDao{
	
	
	
	
	public PageDTO findPager( RegisterCodeDTO dto, int start, int limit, String sidx, String sord )
    {
		
        final DetachedCriteria dc = DetachedCriteria.forClass( RegisterCode.class );

        if ( dto != null )
        {
        	if(dto.getId() != 0){
        		dc.add(Restrictions.eq( "id",dto.getId()) );
        	}
        	//备注
        	if(StringUtils.hasText( dto.getRemark())){
        		 dc.add(Restrictions.like( "remark",dto.getRemark(),MatchMode.ANYWHERE ) );
        	}
        	//激活码
            if ( StringUtils.hasText( dto.getCode()) )
            {
                dc.add(Restrictions.like( "code",dto.getCode(),MatchMode.ANYWHERE ) );
            }
            //状态
            if ( StringUtils.hasText( dto.getStatus()) )
            {
                dc.add(Restrictions.eq( "status",dto.getStatus()));
            }
            //凭证
            if(StringUtils.hasText(dto.getToken())){
            	dc.add(Restrictions.like("token", dto.getToken(),MatchMode.ANYWHERE ));
            }
        }

        //排序
        if ( ( sord != null ) && ! sord.equals( "" ) && ( sidx != null ) && ! sidx.equals( "" ) )
        {
            //add mars 
            if ( sidx.equals( "toRequest" ) )
            {
                sidx = "isToRequest";
            }
            if("createTime".equals(sidx)){
            	sidx = "createTime";
            }

            if ( sord.equals( "desc" ) )
            {
                dc.addOrder( Order.desc( sidx ) );
            } else{
                dc.addOrder( Order.asc( sidx ) );
            }
        }

        return super.findPageByCriteria( dc, start, limit );
    }
}
